package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Permission;
import com.atguigu.service.PermissionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author 杨林
 * @create 2022-09-29 16:01 星期四
 * description:
 */
@Controller //这里不能用 RestController()  因为这里用的是模板引擎而不是ajax
@RequestMapping("/permission")
public class PermissionController extends BaseController {

    @Reference
    private PermissionService permissionService;

    /**
     * 获取菜单并跳转值菜单管理首页
     * @param model
     * @return
     */
    @RequestMapping()
    public String index(ModelMap model){
        //获取菜单
        List<Permission> list = permissionService.findAllMenu();
        model.addAttribute("list",list);
        return "permission/index";
    }

    @RequestMapping("/create")
    public String create(Model model,Permission permission){
        //将前端接收到的参数存入共享域中
        model.addAttribute("permission",permission);
        return "permission/create";
    }

    @RequestMapping("/save")
    public String save(Permission permission){

        //System.out.println(permission);
        //入库
        permissionService.insert(permission);

        return "common/successPage";
    }


    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id,Model model){
        //根据id获取permission
        Permission permission = permissionService.findPermissionByPermissionId(id);
        model.addAttribute("permission",permission);
        return "permission/edit";
    }

    @RequestMapping("/update")
    public String update(Permission permission){
        permissionService.update(permission);
        return "common/successPage";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        permissionService.delete(id);
        return "redirect:/permission";
    }
}
